package vn.gq.udv.utils;

import vn.gq.udv.template.NoNetworkActivity;

import com.actionbarsherlock.app.SherlockActivity;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;


public class NetworkState {
	
	private Context context;
	
	public NetworkState(Context context) {
		this.context = context;
	}
	
	public boolean hasConnection() {
		ConnectivityManager conmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeInfo = conmgr.getActiveNetworkInfo();
		if (activeInfo != null) {
			if (activeInfo.getState() == State.CONNECTED || (activeInfo.getState() == State.CONNECTING)) {
				return true;
			}
		}
		return false;
	}
	
	public void check() {
		ConnectivityManager conmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeInfo = conmgr.getActiveNetworkInfo();
		if (activeInfo != null) {
			if (activeInfo.getState() == State.CONNECTED || (activeInfo.getState() == State.CONNECTING)) {
				return;
			}
		}
		((SherlockActivity) context).finish();
		Intent intent = new Intent(context, NoNetworkActivity.class);
		intent.putExtra("INTENT", ((SherlockActivity) context).getIntent());
		context.startActivity(intent);
	}
}
